//
// Created by Poyyo on 2021/5/25.
//

#include "GraphInListImpl.h"

/**
 * 构造函数
 */
GraphInListImpl::GraphInListImpl() {
    /*
     * number 图中结点的个数
     * temp 临时存储每个结点的邻接结点个数
     * m->n 表示边
     */
    int number, temp, m, n;
    cout << "input the number of the VertexNode:";
    cin >> number;

    // 初始化结点
    for (int i = 1; i <= number; i++) {
        list.insert({i, {i}});
    }

    /*
     * 构造邻接表
     * m: 表示现在输入的是结点m的边
     * temp: 表示结点m边的条数
     */
    cout << "input e:" << endl;
    for (int i = 0; i < number; i++) {
        cin >> m >> temp;
        auto vertex = list.find(m);
        if (vertex != list.end()) {
            for (int j = 0; j < temp; j++) {
                cin >> n;
                if (list.find(n) == list.end()) {
                    continue;
                } else {
                    auto insertedNode = new Node(n);
                    if (vertex->second.nodes == nullptr) {
                        vertex->second.nodes = insertedNode;
                    } else {
                        Node *tempNode = vertex->second.nodes;
                        while (tempNode->next != nullptr) {
                            tempNode = tempNode->next;
                        }
                        tempNode->next = insertedNode;
                    }
                }
            }
        }
    }
}

void GraphInListImpl::ToString() {
    for (int i = 1; i <= list.size(); i++) {
        cout << list.find(i)->first << ": ";
        auto temp = list.find(i)->second.nodes;
        while (temp != nullptr) {
            cout << temp->id << " ";
            temp = temp->next;
        }
        cout << endl;
    }
}

void GraphInListImpl::Select(int id) {
    auto find = list.find(id);
    if (find != list.end()) {
        find->second.vertex.setSelected(true);
    }
}

void GraphInListImpl::Visit(int id) {
    cout << id << " ";
}

const unordered_map<int, VertexNode> &GraphInListImpl::getList() const {
    return list;
}
